#include "pagetrainnumset.h"
#include "ui_pagetrainnumset.h"

PageTrainNumSet::PageTrainNumSet(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageTrainNumSet)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);

    m_pKeyBoard = new SlimNumKeyBoard(this);
    m_pKeyBoard->move(180,520);
    m_pKeyBoard->setFocusPolicy(Qt::NoFocus);
    connect(m_pKeyBoard,SIGNAL(setNumber(int)),this,SLOT(input_slot(int)));
    connect(m_pKeyBoard,SIGNAL(backspace()),this,SLOT(backspace_slot()));
    connect(m_pKeyBoard,SIGNAL(cancel()),this,SLOT(cancel_slot()));
    m_pKeyBoard->hide();

    inputlist.append(ui->lineEdit_LineNo);
    inputlist.append(ui->lineEdit_TrainNo);
    for(int i=0; i<inputlist.size(); ++i)
        inputlist.at(i)->installEventFilter(this);

    setPageActive(false);
}

PageTrainNumSet::~PageTrainNumSet()
{
    delete ui;
}

void PageTrainNumSet::setPageActive(bool xActive)
{
    ui->Btn_LineNoSet->setEnabled(xActive);
    ui->Btn_TrainNoSet->setEnabled(xActive);
}

bool PageTrainNumSet::eventFilter(QObject *obj, QEvent *event)
{
    for(int i=0; i<inputlist.size(); ++i)
    {
        if(event->type() == QEvent::MouseButtonPress)
        {
            if(obj == inputlist.at(i))
            {
                inputlist.at(i)->setStyleSheet("background-color: rgb(100,255,255); border: 1px solid white;font:20px 'Arial';");
                m_pFocusLinedit = inputlist.at(i);
                m_pKeyBoard->show();
            }
            else
                inputlist.at(i)->setStyleSheet("background-color: white; border: 1px solid white;font:20px 'Arial';");
        }
    }
    return QWidget::eventFilter(obj, event);
}

void PageTrainNumSet::input_slot(int number)
{
    QKeyEvent numKey(QEvent::KeyPress, (Qt::Key)(Qt::Key_0+number), Qt::NoModifier, QString::number(number));
    QApplication::sendEvent(m_pFocusLinedit, &numKey);
}

void PageTrainNumSet::backspace_slot()
{
    QKeyEvent backspaceKey(QEvent::KeyPress,Qt::Key_Backspace,Qt::NoModifier);
    QApplication::sendEvent(m_pFocusLinedit,&backspaceKey);
}

void PageTrainNumSet::cancel_slot()
{
    m_pKeyBoard->hide();
}

void PageTrainNumSet::on_Btn_Return_clicked()
{
    emit returnToMaintMenu();
}

void PageTrainNumSet::on_Btn_LineNoSet_clicked()
{
    emit signal_LineSetValid(true);
    ui->Btn_LineNoSet->setEnabled(false);
    QTimer::singleShot(2000,this,[=](){
        emit signal_LineSetValid(false);
        ui->Btn_LineNoSet->setEnabled(true);
    });
}

void PageTrainNumSet::on_Btn_TrainNoSet_clicked()
{
    emit signal_TrainIDSetValid(true);
    ui->Btn_TrainNoSet->setEnabled(false);
    QTimer::singleShot(2000,this,[=](){
        emit signal_TrainIDSetValid(false);
        ui->Btn_TrainNoSet->setEnabled(true);
    });
}

uint8_t PageTrainNumSet::getLineIDInput()
{
    if(ui->lineEdit_LineNo->text().isEmpty())
        return 0;
    return ui->lineEdit_LineNo->text().toUShort();
}

uint8_t PageTrainNumSet::getTrainIDInput()
{
    if(ui->lineEdit_TrainNo->text().isEmpty())
        return 0;
    return ui->lineEdit_TrainNo->text().toUShort();
}

void PageTrainNumSet::setLineID(uint8_t id)
{
    ui->label_LineID->setText(QString().setNum(id));
}

void PageTrainNumSet::setTrainID(uint8_t id)
{
    ui->label_TrainID->setText(QString().setNum(id));
}
